﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_KhuyenMaiVang.Models;
using Project_KhuyenMaiVang.DataProvider;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace Project_KhuyenMaiVang.DAO
{
    public class CommentDAO : MyDataProvider
    {
 


        // lấy tất cả comment liên quan đến tin khuyến mãi
        // return danh sách comment
        public List<CommentDTO> getListCommentsBy(int MaTinKhuyenMai)
        {
            try
            {
                List<CommentDTO> result = new List<CommentDTO>();
                string query = "SELECT * FROM COMMENT WHERE MATINKHUYENMAI = " + MaTinKhuyenMai
                                + " order by datediff(d, NgayComment, getdate()) desc";
                _Adapter = new SqlDataAdapter(query, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    object ob = GetDataFromDataRow(dt, i);
                    result.Add((CommentDTO)ob);
                }
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// Get all
        /// </summary>
        /// <returns></returns>
        public List<CommentDTO> getAllComments()
        {
            try
            {
                List<CommentDTO> result = new List<CommentDTO>();
                string query = "SELECT * FROM COMMENT";
                _Adapter = new SqlDataAdapter(query, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    object ob = GetDataFromDataRow(dt, i);
                    result.Add((CommentDTO)ob);
                }
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }



        public CommentDTO getCommentBy(int maTin, int idAccount, string noiDung)
        {
            try
            {
                List<CommentDTO> result = new List<CommentDTO>();
                string query = "SELECT * FROM COMMENT WHERE MaTinKhuyenMai = '" + maTin + "' and IDAccount = '" + idAccount + "' and NoiDung = '" + noiDung + "'";
                _Adapter = new SqlDataAdapter(query, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];


                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    object ob = GetDataFromDataRow(dt, i);
                    result.Add((CommentDTO)ob);
                }
                return result[0];
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            CommentDTO commentDTO = new CommentDTO();
            commentDTO.IDComment = Int32.Parse(dt.Rows[i]["IDComment"].ToString());
            commentDTO.MaTinKhuyenMai = Int32.Parse(dt.Rows[i]["MaTinKhuyenMai"].ToString());
            commentDTO.IDAccount = Int32.Parse(dt.Rows[i]["IDAccount"].ToString());
            commentDTO.NoiDung = dt.Rows[i]["NoiDung"].ToString();
            commentDTO.NgayComment = DateTime.Parse(dt.Rows[i]["NgayComment"].ToString());

            return (object)commentDTO;
        }

        // Xóa comment trong database
        // return true nếu thành công, false nếu thất bại
        public bool delete(CommentDTO commentDTO)
        {
            bool isOK = true;
            try
            {
                string deleteCommand = "DELETE FROM COMMENT WHERE IDCOMMENT = " + commentDTO.IDComment.ToString();
                executeNonQuery(deleteCommand);
            }
            catch
            {
                isOK = false;
            }

            return isOK;
        }

        public bool delete(int maTin, int idAccount, string noiDung)
        {
            bool isOK = true;
            try
            {
                string deleteCommand = "DELETE FROM COMMENT WHERE MATINKHUYENMAI = " + maTin +
                                                                 "and IDACCOUNT = " + idAccount +
                                                                 "and NOIDUNG = N'" + noiDung + "'";
                executeNonQuery(deleteCommand);
            }
            catch
            {
                isOK = false;
            }

            return isOK;
        }

        // Thêm 1 comment vào bảng comment
        // parameter    : CommentDTO
        // return       : true nếu thành công, false nếu thất bại
        public bool insertComment(CommentDTO comment )
        {
            bool isOK = true;
            try
            {
                string insertCommand = "INSERT INTO Comment VALUES(" +
                                       //comment.IDComment + "," +
                                       comment.MaTinKhuyenMai + "," +
                                       comment.IDAccount + ", N'" +
                                       comment.NoiDung + "', " +
                                       "convert(datetime,'" + comment.NgayComment.ToShortDateString() + "',103))";
                executeNonQuery(insertCommand);
            }
            catch
            {
                isOK = false;
            }

            return isOK;
        }

        // Cập nhật comment dựa theo mã Comment
        // parameter    : CommentDTO
        // return       : true nếu thành công, false nếu thất bại
        public bool updateComment(CommentDTO comment)
        {
            try
            {
                string query = "update Comment "
                    + "set NoiDung = '" + comment.NoiDung.ToString() + "', "
                    + "NgayComment = '" + comment.NgayComment.ToString() + "' "
                    + "where IDComment = " + comment.IDComment.ToString();
                this.executeNonQuery(query);
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
    }
}